package com.baturu.vin.benz.transformer;

import com.baturu.vin.benz.consts.BenzConstant;
import com.baturu.vin.benz.dto.BenzSuperMainGroupDTO;
import com.baturu.vin.benz.dto.BenzSuperSubGroupDTO;
import com.baturu.vin.dto.superEPC.SEPCExtendAttributeDTO;
import com.baturu.vin.dto.superEPC.SEPCGroupDTO;
import com.google.common.collect.Lists;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

/**
 * BenzAttributeTransformerService
 * 配置属性转换Service
 *
 * @author majianming
 * @date 2019-05-06 13:53
 */
@Service
public class BenzAttributeTransformerService {

    @Autowired
    private BenzGroupTransformerService benzMainGroupToMainGroup;

    /**
     * 获取拓展属性中的 baseCode 属性 作为车型的
     *
     * @param extendAttrDTOs
     * @return
     */
    String getBaseCode(List<SEPCExtendAttributeDTO> extendAttrDTOs) {
        String baseCode = null;
        if (CollectionUtils.isNotEmpty(extendAttrDTOs)) {
            Optional<SEPCExtendAttributeDTO> baseAttribute = extendAttrDTOs.stream().filter(l -> StringUtils.equals(BenzConstant.BASE_CODE_KEY, l.getCode())).findFirst();
            if (baseAttribute.isPresent() && (baseAttribute.get().getValue() instanceof String)) {
                baseCode = (String) baseAttribute.get().getValue();
            }
        }
        return baseCode;
    }


    List<SEPCExtendAttributeDTO> toBaseCode(BenzSuperSubGroupDTO benzSuperSubGroupDTO) {
        if (Objects.isNull(benzSuperSubGroupDTO) || StringUtils.isBlank(benzSuperSubGroupDTO.getBaseCode())) {
            return Collections.emptyList();
        }
        return Lists.newArrayList(SEPCExtendAttributeDTO.builder().code(BenzConstant.BASE_CODE_KEY).value(benzSuperSubGroupDTO.getBaseCode()).build());
    }

    List<SEPCExtendAttributeDTO> toBaseCode(BenzSuperMainGroupDTO benzSuperMainGroupDTO) {
        if (Objects.isNull(benzSuperMainGroupDTO) || StringUtils.isBlank(benzSuperMainGroupDTO.getBaseCode())) {
            return Collections.emptyList();
        }
        return Lists.newArrayList(SEPCExtendAttributeDTO.builder().code(BenzConstant.BASE_CODE_KEY).value(benzSuperMainGroupDTO.getBaseCode()).build());
    }

    List<SEPCExtendAttributeDTO> toAttribute(List<BenzSuperMainGroupDTO> benzSuperMainGroupDTOS) {
        List<SEPCGroupDTO> groupDTOS = benzMainGroupToMainGroup.benzMainGroupToMainGroup(benzSuperMainGroupDTOS);
        if (CollectionUtils.isEmpty(groupDTOS)) {
            return Collections.emptyList();
        }
        return Lists.newArrayList(SEPCExtendAttributeDTO.builder().code("extendMainGroups").value(groupDTOS).build());
    }
}
